package common.ComboboxCheckboxTree
{ 
	import mx.controls.Tree;
	import mx.core.ClassFactory;
	import mx.events.ListEvent;
	
	/**
	 * 
	 * @author zhangzhicheng
	 * 
	 */	
	public class TreeComboBoxRenderer extends Tree 
	{ 
		
		[Bindable] 
		public var outerDocument:TreeComboBox; 
		
		public function TreeComboBoxRenderer() 
		{ 
			super(); 
			this.itemRenderer = new ClassFactory(CheckBoxTreeRenderer);
			this.addEventListener(ListEvent.CHANGE, onSelectionChanged); 
			this.setStyle("defaultLeafIcon", null); 
			this.setStyle("folderClosedIcon", null); 
			this.setStyle("folderOpenIcon", null); 
		} 
		
		/**
		 * 选中项目更改
		 */		
		private function onSelectionChanged(event:ListEvent):void 
		{ 
			outerDocument.updateLabel(event.currentTarget.selectedItem); 
		} 
		
		/**
		 * 打开父结点
		 */		
		public function expandParents(node:Object):void 
		{ 
			if (node && !isItemOpen(node)) 
			{ 
				expandItem(node, true); 
				//var pnode:Object = this.getParentItem(node);
				//expandParents(pnode); 
				expandParents(node.owner);
			}        
		} 
		
		/**
		 * 设置选中节点 
		 */		
		public function selectNode(node:Object):void 
		{ 
			selectedItem = node; 
			var idx:int = getItemIndex(selectedItem); 
			scrollToIndex(idx); 
		} 
		
		/**
		 * 是否可以选择
		 */		
		override public function isItemSelectable(data:Object):Boolean 
		{ 
			return  super.isItemSelectable(data) && outerDocument.isSelectable(data); 
		} 
		
		/**
		 * 更新选中项目的数组
		 */		
		public function updateSelectedItems(data:Object, select:Boolean):void
		{
			if (select) 
			{
				treeSelectedItems.push(data);
				if (outerDocument.treeSelectedItem == null)
				{
					outerDocument.treeSelectedItem = data;
				}
			}
			else
			{
				treeSelectedItems.splice(treeSelectedItems.indexOf(data), 1);
			}
		}
		
		/**
		 * 选中项目数组
		 */		
		public function get treeSelectedItems():Array
		{
			return outerDocument.treeSelectedItems;
		}
		
	} 
}